//
//  CABaseAnimationTouchMoveViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/26.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "CABaseAnimationTouchMoveViewController.h"

@interface CABaseAnimationTouchMoveViewController ()<CAAnimationDelegate>
/** 红色View */
@property (nonatomic, strong) UIView  *AniamtionView;
@end
static NSString * const redViewKey = @"redViewKey";
static NSString * const redViewMovePositionKey = @"redViewMovePositionKey";
@implementation CABaseAnimationTouchMoveViewController
/** 红色动画View */
- (UIView *)AniamtionView{
    if(!_AniamtionView){
        _AniamtionView = [UIView new];
        _AniamtionView.backgroundColor = [UIColor redColor];
        _AniamtionView.frame = CGRectMake(100, 100, 80, 80);
        _AniamtionView.layer.cornerRadius = 6;
        _AniamtionView.layer.masksToBounds = YES;
    }
    return  _AniamtionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"moveAnimation";
    [self.view addSubview:self.AniamtionView];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [touches.anyObject locationInView:self.view];
    [self moveAniamtionWithCGPoint:point];
}
//移动
- (void)moveAniamtionWithCGPoint:(CGPoint)movePoint{
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    //到达位置
    baseAnimation.toValue = [NSValue valueWithCGPoint:movePoint];
    //移动事件
    baseAnimation.duration = 1.0f;
    //动画完成后是否以动画的形式回到初始值
    baseAnimation.autoreverses = NO;
    //图层是否显示执行后的动画执行后的位置以及状态
    baseAnimation.removedOnCompletion = NO;
    baseAnimation.fillMode = kCAFillModeForwards;
    //设置代理
    baseAnimation.delegate = self;
    //保存值
    [baseAnimation setValue:[NSValue valueWithCGPoint:movePoint] forKey:redViewMovePositionKey];
    //时间偏移(设置后会一闪一闪的，从起点到终点的闪动效果)
//    baseAnimation.timeOffset = 0.04;
    [self.AniamtionView.layer addAnimation:baseAnimation forKey:redViewKey];
}
#pragma mark --- <CAAnimationDelegate>
- (void)animationDidStart:(CAAnimation *)anim{
    if ([self.AniamtionView.layer animationForKey:redViewKey] == anim) {
        NSLog(@"红色View开始移动");
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([self.AniamtionView.layer animationForKey:redViewKey] == anim) {
        NSLog(@"红色View停止移动");
        //开始事物
        [CATransaction begin];
    
        //关闭隐士动画
        [CATransaction setDisableActions:YES];
        //红色View轨迹
        self.AniamtionView.layer.position = [[anim valueForKey:redViewMovePositionKey]CGPointValue];
        //提交事物
        [CATransaction commit];
        /*
         非根图层，设置它的可动画属性是有隐式动画的，那么我们需要关闭图层的隐式动画，我们就需要用到动画事务CATransaction：
         
         说到这，我们就简单介绍一下CATransaction，有人说，我好像没见过这个东西，他是个什么鬼？和NSAutoreleasePool一样，当我们不手动创建时，系统会在一帧开始时生成一个事务，并在这一帧结束时commit，这也就是隐式CATransaction。当然你也可以利用[CATransaction begin]方法开始，调用[CATransaction commit]方法结束，中间便是事务的作用域，然后把需要更改可动画属性的操作放在该作用域内，这就是显式CATransaction，它常常用于关闭隐式动画和调整动画时间。下面我们就用它来关闭修改图层的position时所带来的隐式动画
         */
        
        
    }
}
@end
